﻿using CPF;
using CPF.Controls;
using CPF.Input;
using ScottPlot;
using ScottPlot.CPF;
using ScottPlot.Plottables;

namespace Demo
{
    public class Window1 : Window
    {
        public Crosshair CH
        {
            get { return GetValue<Crosshair>(); }
            set
            {
                SetValue(value);
            }
        }


        public static double[,] MonaLisa()
        {
            int size = 50;
            double[,] matrix = new double[size, size];
            Random random = new Random();

            for (int i = 0; i < size; i++)
            {
                for (int j = 0; j < size; j++)
                {
                    // 填充随机双精度浮点数，范围是 [0.0, 1.0)
                    matrix[i, j] = random.NextDouble();
                }
            }

            return matrix;
        }

        protected override void InitializeComponent()
        {
            Icon = "res://Demo/icon.png";

            LoadStyleFile("res://Demo/Stylesheet1.css");
            //加载样式文件，文件需要设置为内嵌资源

            Title = "标题";
            Width = 992;
            Height = 543;
            Background = null;
            CanResize = true;
            Children.Add(new WindowFrame(this, new Panel
            {
                Width = "100%",
                Height = "100%",
                Children = 
                { 
                    new CPFPlot
                    {
                        PresenterFor = this,
                        Name = "CPFPlot",
                        Width = "100%",
                        Height = "100%",
                        [nameof(CPFPlot.MouseMove)]=new CommandDescribe((s,e)=>
                        {
                            CPFPlot cpfPlot = (CPFPlot)s;
                            MouseEventArgs me = (MouseEventArgs)e;
                            Pixel mousePixel = new(me.Location.X, me.Location.Y);
                            Coordinates mouseCoordinates = cpfPlot.Plot.GetCoordinates(mousePixel);
                            CH.Position = mouseCoordinates;
                            CH.VerticalLine.Text = $"{mouseCoordinates.X:N3}";
                            CH.HorizontalLine.Text = $"{mouseCoordinates.Y:N3}";
                            cpfPlot.Refresh();
                        }),
                    }
                }
            }));
            if (!DesignMode)//设计模式下不执行，也可以用#if !DesignMode
            {
                
            }
        }

        protected override void OnInitialized()
        {
            base.OnInitialized();
            var cpfPlot = this.FindPresenterByName<CPFPlot>("CPFPlot");
            cpfPlot.UserInputProcessor.IsEnabled = true;

            var data = MonaLisa();

            var hm1 = cpfPlot.Plot.Add.Heatmap(data);
            hm1.Colormap = new ScottPlot.Colormaps.Turbo();
            //hm1.Smooth = true;
            cpfPlot.Plot.Add.ColorBar(hm1);
            cpfPlot.Plot.Axes.Hairline(true);
            cpfPlot.Plot.XLabel("Horizonal Axis");
            cpfPlot.Plot.YLabel("Vertical Axis");
            cpfPlot.Plot.Title("Plot Title");

            CH = cpfPlot.Plot.Add.Crosshair(0, 0);
            CH.TextColor = Colors.White;
            CH.TextBackgroundColor = CH.HorizontalLine.Color;

            cpfPlot.Refresh();
        }

    }
}
